package com.cj.mobile.iabp.mvp.presenter;

import static com.cj.mobile.iabp.BuildConfig.IS_DEBUG_DATA;

import com.cj.mobile.iabp.app.config.CommonRetryWithDelay;
import com.cj.mobile.iabp.app.global.AccountManager;
import com.cj.mobile.iabp.app.utils.RxUtils;
import com.cj.mobile.iabp.mvp.contract.BasicInfoContract;
import com.cj.mobile.iabp.mvp.model.entity.AccountInfo;
import com.cj.mobile.iabp.mvp.model.entity.DeptInfo;
import com.jess.arms.cj.CommonResponse;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.RxLifecycleUtils;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 07/29/2020 18:00
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class BasicInfoPresenter extends BasePresenter<BasicInfoContract.Model, BasicInfoContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    AccountManager mAccountManager;

    @Inject
    public BasicInfoPresenter(BasicInfoContract.Model model, BasicInfoContract.View rootView) {
        super(model, rootView);
    }

    /**
     * 初始化信息
     */
    public void initData() {
        getUserInfo();
    }

    private void getUserInfo() {
        if (IS_DEBUG_DATA) {
            mRootView.onLoadSuccess(new AccountInfo(
                    "1", "杨津", "1", "y423b4445071644a527a", "3941ca9ac15f3390f6c1511b8373b1e7", "0", "", "", "12335",
                    "15081865467", "33", "2025-05-10 09:18:54", "2025-05-10 09:18:54", "1", "2025-05-10 09:18:54",
                    "", "0", "0", "2", "CCU", "CCU", "朝阳院区", "1", -1
            ));

            getCampusList();
        } else {
            mModel.getUserInfo()
                    .compose(RxUtils.applySchedulers(mRootView, false, true))                                    // 使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                    .subscribe(new ErrorHandleSubscriber<AccountInfo>(mErrorHandler) {
                        @Override
                        public void onError(Throwable t) {
                            super.onError(t);
                            getCampusList();
                        }

                        @Override
                        public void onNext(AccountInfo info) {
                            mRootView.onLoadSuccess(info);
                            getCampusList();
                        }
                    });

        }
    }

    /**
     * 获取院区
     */
    private void getCampusList() {
        if (IS_DEBUG_DATA) {
            List<DeptInfo> mList = new ArrayList<>();
            mList.add(new DeptInfo(2, 1, "朝阳院区", "1", "2025-03-19 11:11:42", "admin", "2025-03-19 17:34:11", "admin", "5", "0"));
            mList.add(new DeptInfo(3, 1, "通州院区", "1", "2025-03-19 11:11:42", "admin", "2025-03-19 17:34:11", "admin", "5", "0"));

            mRootView.loadCampus(mList);
        } else {
            mModel.getDeptList("1")
                    .compose(RxUtils.applySchedulers(mRootView, true, false))                                    // 使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                    .subscribe(new ErrorHandleSubscriber<List<DeptInfo>>(mErrorHandler) {

                        @Override
                        public void onNext(List<DeptInfo> info) {
                            mRootView.loadCampus(info);
                        }
                    });

        }
    }

    /**
     * 获取科室列表
     */
    public void getDepartmentList(String parentId) {
        if (IS_DEBUG_DATA) {
            List<DeptInfo> mList = new ArrayList<>();
            if (parentId.equals("2")) {
                // 朝阳院区
                mList.add(new DeptInfo(4, 2, "主动脉外科中心二区", "1", "2025-03-19 11:11:42", "admin", "2025-03-19 17:34:11", "admin", "5", "0"));
                mList.add(new DeptInfo(5, 2, "主动脉外科中心四区", "1", "2025-03-19 11:11:42", "admin", "2025-03-19 17:34:11", "admin", "5", "0"));
                mList.add(new DeptInfo(6, 2, "冠心病外科中心二区", "1", "2025-03-19 11:11:42", "admin", "2025-03-19 17:34:11", "admin", "5", "0"));
                mList.add(new DeptInfo(7, 2, "冠心病外科中心四区", "1", "2025-03-19 11:11:42", "admin", "2025-03-19 17:34:11", "admin", "5", "0"));
                mList.add(new DeptInfo(8, 2, "小儿心脏科中心二区", "1", "2025-03-19 11:11:42", "admin", "2025-03-19 17:34:11", "admin", "5", "0"));
                mList.add(new DeptInfo(9, 2, "心真与膜外科中心", "1", "2025-03-19 11:11:42", "admin", "2025-03-19 17:34:11", "admin", "5", "0"));

            } else if (parentId.equals("3")) {
                // 通州院区
                mList.add(new DeptInfo(14, 3, "心外监护室三区", "1", "2025-03-19 11:11:42", "admin", "2025-03-19 17:34:11", "admin", "5", "0"));
                mList.add(new DeptInfo(15, 3, "心外监护室一区", "1", "2025-03-19 11:11:42", "admin", "2025-03-19 17:34:11", "admin", "5", "0"));
                mList.add(new DeptInfo(16, 3, "微创外科中心一区", "1", "2025-03-19 11:11:42", "admin", "2025-03-19 17:34:11", "admin", "5", "0"));
                mList.add(new DeptInfo(17, 3, "结构性心脏病中心一区", "1", "2025-03-19 11:11:42", "admin", "2025-03-19 17:34:11", "admin", "5", "0"));
                mList.add(new DeptInfo(18, 3, "瓣膜与房颜外科中心", "1", "2025-03-19 11:11:42", "admin", "2025-03-19 17:34:11", "admin", "5", "0"));
                mList.add(new DeptInfo(19, 3, "小儿心脏科中心-区", "1", "2025-03-19 11:11:42", "admin", "2025-03-19 17:34:11", "admin", "5", "0"));
            }

            mRootView.loadDepartment(mList);
        } else {
            mModel.getDeptList(parentId)
                    .compose(RxUtils.applySchedulers(mRootView, true, false))                                    // 使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                    .subscribe(new ErrorHandleSubscriber<List<DeptInfo>>(mErrorHandler) {

                        @Override
                        public void onNext(List<DeptInfo> info) {
                            mRootView.loadDepartment(info);
                        }
                    });

        }
    }

    /**
     * 提交事件
     *
     * @param id      用户ID
     * @param name    姓名
     * @param sex     性别
     * @param phone   电话
     * @param deptSec 院区id
     * @param deptId  科室id
     */
    public void btnSubmit(String id, String name, String sex, String phone, String deptSec, String deptId) {
        if (IS_DEBUG_DATA) {
            mRootView.showMessage("信息维护成功！");

            mAccountManager.setUserName(name);
        } else {
            // 基本信息
            mModel.updateUserInfo(id, name, sex, phone, deptSec, deptId)
                    .subscribeOn(Schedulers.io())
                    .retryWhen(new CommonRetryWithDelay(2, 2))    // 遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
                    .doOnSubscribe(disposable -> {
                        mRootView.showLoadingSubmit();
                    })
                    .subscribeOn(AndroidSchedulers.mainThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .doFinally(() -> {
                        mRootView.hideLoadingSubmit();
                    }).compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                    .subscribe(new ErrorHandleSubscriber<CommonResponse>(mErrorHandler) {

                        @Override
                        public void onNext(CommonResponse commonResponse) {
                            mRootView.showMessage("信息维护成功！");

                            mAccountManager.setUserName(name);
                        }
                    });

        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAccountManager = null;
    }
}
